package com.zy.fee.roadpark.dao;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Base64;
import com.nanchen.compresshelper.CompressHelper;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.JsonObjectRequest;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.app.AppApplication;
import com.zy.fee.roadpark.config.Urls;
import com.zy.fee.roadpark.model.apimodel.APIM_AdminInfo;
import com.zy.fee.roadpark.model.apimodel.APIM_LotInfo;
import com.zygk.library.model.CommonResult;
import com.zygk.library.model.M_ImageUpload;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONObject;

/**
 * Created by Wikison on 2018/9/13.
 */
public class CommonLogic {
  //登录
  public static void login(Context mContext, String LoginName, String Password,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_ADMIN_LOGIN, RequestMethod.POST);
    request.add("LoginName", LoginName);
    request.add("Password", Password);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_AdminInfo result =
            JsonUtil.jsonToObject(response.get().toString(), APIM_AdminInfo.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          callback.onFailed();
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  //修改密码
  public static void password_edit(Context mContext, String adminID, String password,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_PASSWORD_EDIT, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("password", password);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_AdminInfo result =
            JsonUtil.jsonToObject(response.get().toString(), APIM_AdminInfo.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          callback.onFailed();
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 上传图片
   */
  public static void do_upload_pic(final Context mContext, String path, boolean compress,
      final HttpRequest.HttpCallback callback) {
    File newFile;

    if (compress) {
      try {
        newFile =
            CompressHelper.getDefault(AppApplication.getContext()).compressToFile(new File(path));
      } catch (Exception e) {
        ToastUtil.showMessage(mContext, "该图片格式错误，请重新选择！");
        return;
      }
    } else {
      newFile = new File(path);
    }
    StringRequest request = new StringRequest(Urls.DO_UPLOAD_PIC, RequestMethod.POST);
    request.add("filename", new FileBinary(newFile));
    request.add("projectName", "");
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) {
          callback.onFailed();
          return;
        }

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 上传图片
   */
  public static void do_upload_pic(final Context mContext, boolean compress, String imagePath,
      String fileName,
      final HttpRequest.HttpCallback callback) {

    InputStream in = null;
    byte[] data = null;

    File oldFile = new File(imagePath);
    if (oldFile == null) return;

    File newFile;
    if (compress) {
      try {
        newFile =
            new CompressHelper.Builder(mContext).setMaxWidth(800).setMaxHeight(800).setQuality(70)
                .setCompressFormat(Bitmap.CompressFormat.JPEG) // 设置默认压缩为jpg格式
                .setDestinationDirectoryPath(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        .getAbsolutePath()).build().compressToFile(oldFile);
      } catch (Exception e) {
        ToastUtil.showMessage(mContext, "该图片格式错误，请重新选择！");
        callback.onFailed();
        return;
      }
    } else {
      newFile = new File(imagePath);
    }

    // 读取图片字节数组
    try {
      in = new FileInputStream(newFile);
      data = new byte[in.available()];
      in.read(data);
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    String picData = Base64.encodeToString(data, Base64.DEFAULT);
    JsonObjectRequest request = new JsonObjectRequest(Urls.DO_UPLOAD_PIC_HW, RequestMethod.POST);
    M_ImageUpload req = new M_ImageUpload(picData, fileName, 0);
    request.setDefineRequestBodyForJson(JsonUtil.objectToJson(req));

    SimpleResponseListener<JSONObject> listener = new SimpleResponseListener<JSONObject>() {
      @Override
      public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override
      public void onSucceed(int what, Response<JSONObject> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get().toString(), CommonResult.class);

        if (result == null) {
          callback.onFailed();
          return;
        }

        if (result.isResult() == true) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          callback.onFailed();
          return;
        }
      }

      @Override
      public void onFailed(int what, Response<JSONObject> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override
      public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  // 停车场详情
  public static void lotInfo(Context mContext, String lotID,
                           final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.LOT_INFO, RequestMethod.POST);
    request.add("lotID", lotID);
    request.add("center", "119.969499,31.817949");
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_LotInfo result =
          JsonUtil.jsonToObject(response.get().toString(), APIM_LotInfo.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          callback.onFailed();
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
